package com.qxzn.app.page.widget;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.qxzn.app.page.R;

public class ToastUtil extends Toast {
    /**
     * Construct an empty Toast object.  You must call {@link #setView} before you
     * can call {@link #show}.
     *
     * @param context The context to use.  Usually your {@link Application}
     *                or {@link Activity} object.
     */
    public ToastUtil(Context context) {
        super(context);
    }

    public static Toast showTextToast(Context context, String tvStr) {
        return showImgToast(context, tvStr, 0);
    }

    public static Toast showImgToast(Context context, String tvStr, int imageResource) {
        context = context.getApplicationContext();
        Toast toast = new Toast(context);
        View view;
        if (imageResource > 0) {
            view = LayoutInflater.from(context).inflate(R.layout.toast_img, null);
            ((ImageView) view.findViewById(R.id.iv_icon)).setImageResource(imageResource);
        } else {
            view = LayoutInflater.from(context).inflate(R.layout.toast_text, null);
        }
        TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
        tv_message.setText(TextUtils.isEmpty(tvStr) ? "" : tvStr);
        toast.setView(view);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
        return toast;
    }

}
